<?php

class Db{
   private $host;
   private $root;
   private $password;
   private $datebase;
   private $port;
   public  $connect;
   function __construct($host="localhost",$root,$password,$datebase,$port='8080')
   {
      $this->host=$host;
      $this->root=$root;
      $this->password=$password;    
      $this->datebase=$datebase; 
      $this->port=$port;  
      $this->connect();
   }
   public function connect(){
       $this->connect=mysqli_connect($this->host,$this->root,$this->password,$this->datebase);
       if($this->connect){
           echo "数据库连接成功";
       }else{
           echo "数据库连接失败".mysqli_error($this->connect);
           return;
       }
   }
   
   public function write($name,$age){
       $sql="insert into user(`name`,`age`) values('".$name."','".$age."')";
       if(mysqli_query($this->connect,$sql)){
           echo "插入成功";
       }else{
           echo "插入失败".mysqli_error($this->connect);
       }
   }
   
   public function del($table,$val,$val1){
      $sql="DELETE FROM $table WHERE $val = $val1";
      if(mysqli_query($this->connect,$sql)){
          echo "删除成功";
      }else{
          echo "删除失败".mysqli_error($this->connect);
      }
   }

   public function select($ret,$table,$val,$val1){
    $sql="SELECT $ret FROM $table WHERE $val = $val1";
    if(mysqli_query($this->connect,$sql)){
        $result=mysqli_fetch_assoc(mysqli_query($this->connect,$sql));
        echo $result[$ret];
    }else{
        echo "查询失败".mysqli_error($this->connect);
    }
 }
 public function edit($table,$val,$v,$val1,$v1){
    $sql="UPDATE $table SET $val='".$v."' where $val1=$v1";
    if(mysqli_query($this->connect,$sql)){
        echo "修改成功";
    }else{
        echo "修改失败".mysqli_error($this->connect);
    }
 }

   



}

$db=new Db('localhost','root','root','book',' 8080');
// $db->write('longlong','40');
// $db->del('user','id','3');
$db->select('id','user','age','20');
$db->edit('user','name','lili','id','1');





?>